package plus.exp;
import java.util.*;

import plus.S_Array;
import plus.Type;
import plus.run.Emit;
public class E_LitArray extends Exp {
	private S_Array solution = new S_Array(Type.variable());
	private Exp[] es;
	public E_LitArray(List<Exp> es) {
		type = Type.fresh(solution);
		this.es = new Exp[es.size()];
		int i=0;
		for (Exp e: es) this.es[i++] = e;
	}
	@Override
	public void threadScope() {
		for (Exp e: es) {
			e.threadScope();
			solution.range = Type.unify(solution.range, e.type);
		}
	}
	@Override String impliedName() { return null; }
	@Override String aThis() { return "a literal array"; }
	@Override
	public void compilePush() {
		// Create new array of indicated length
		Emit.newArray(es.length);
		Emit.loadInt(0);  // Push an index counter.
		for (Exp e: es) {  // for each expression,
			e.compilePush();   // evaluate it,
			Emit.stuffArray(); // store to next fixed offset
		}
		Emit.pop();  // Pop the index counter.
		// this leaves filled array on TOS
	}
}
